# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=cmake
pkgname=("${_realname}" "${_realname}-emacs" "${_realname}-vim")
pkgver=3.15.5
pkgrel=1
pkgdesc="A cross-platform open-source make system"
arch=('i686' 'x86_64')
url="https://www.cmake.org/"
license=("MIT")
makedepends=("gcc" "pkg-config" 'emacs'
             'jsoncpp-devel'
             'libarchive-devel'
             'libbz2-devel'
             'libcurl-devel'
             'libexpat-devel'
             'liblzma-devel'
             'librhash-devel'
             'libutil-linux-devel'
             'libuv-devel'
             'libzstd-devel'
             'ncurses-devel'
             'zlib-devel')
depends=("gcc-libs"
         "jsoncpp"
         "libcurl"
         "libexpat"
         "libarchive"
         "librhash"
         "libutil-linux"
         "libuv"
         "ncurses"
         "pkg-config"
         "zlib")
options=('staticlibs' 'strip')
source=("https://www.cmake.org/files/v${pkgver%.*}/${pkgname}-${pkgver}.tar.gz"
        "cmake-3.15.0-msys.patch"
        "disable-curses-for-msys.patch"
        "disable-response-files-for-msys.patch"
        "3.9.3-cpuinfo.patch"
        "3.15.0-cygwin-paths.patch")
sha256sums=('fbdd7cef15c0ced06bb13024bfda0ecc0dedbcaaaa6b8a5d368c75255243beb4'
            'fe727ac6708d0df1503c241618e2005d9dc16b0853495392e6a86a471fbcced3'
            'b7e398e70b97088f88a0688e8a0794d6780d3cdb91075388a30908dc4b405eb7'
            'ffde15cbf2cbe3f5f3f0301722d9941bfa94396f667fdc911323b8b3f69dfbc6'
            '48995937ea5233137e1ae9c8c0e78911318caf0f35c7ab7a22d1a0f1168d723a'
            'cbe09a39d2def9bc3176c8d60a828ec9a318c6fcf84676f1464ca854b1ee90ab')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${pkgname}-${pkgver}
  del_file_exists Modules/CPackMsys.cmake \
    Modules/FindMsys.cmake \
    Modules/Platform/MSYS-Clang-C.cmake \
    Modules/Platform/MSYS-Clang-CXX.cmake \
    Modules/Platform/MSYS-Determine-CXX.cmake \
    Modules/Platform/MSYS-GNU.cmake \
    Modules/Platform/MSYS.cmake \
    Modules/Platform/MSYS-CXX.cmake \
    Modules/Platform/MSYS-GNU-C.cmake \
    Modules/Platform/MSYS-GNU-CXX.cmake \
    Modules/Platform/MSYS-GNU-Fortran.cmake \
    Modules/Platform/MSYS-windres.cmake || true
  apply_patch_with_msg \
       "cmake-3.15.0-msys.patch" \
       "disable-response-files-for-msys.patch" \
       "3.9.3-cpuinfo.patch" \
       "3.15.0-cygwin-paths.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  MSYSTEM=MSYS "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix=/usr \
    --system-libs \
    --no-qt-gui \
    --parallel=$(nproc) \
    --mandir=share/man \
    --docdir=share/doc/${_realname} \
    -- -DCURSES_FORM_LIBRARY=/usr/lib/libformw.dll.a \
      -DCTEST_TEST_CTEST=ON \
      -DCMAKE_BUILD_TYPE=Release
  #Fix tests
  local  _gccver=$(gcc --version | grep ^gcc | sed 's/^.* //g')
  sed -e "s|4.9.2|${_gccver}|g" -e "s|4.8.2|${_gccver}|g" \
       -i ${srcdir}/build-${CARCH}/Tests/CMakeTests/ImplicitLinkInfoTest.cmake
  plain "Start building..."
  make
  make DESTDIR=$srcdir/dest install
}

#Enable once we can get all tests to pass
#check() {
#  cd "${srcdir}/build-${CARCH}"
#  make test
#}


package_cmake() {
  cd "${srcdir}/dest"
  
  install -d  $pkgdir/usr/bin/
  install -d  $pkgdir/usr/share/aclocal
  install -d  $pkgdir/usr/share/${_realname}-$pkgver
  
  cp -r usr/bin/* $pkgdir/usr/bin/
  cp -r usr/share/aclocal/* $pkgdir/usr/share/aclocal/
  cp -r usr/share/cmake-$pkgver/* $pkgdir/usr/share/cmake-$pkgver/

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/Copyright.txt \
    "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
}

package_cmake-vim() {
  pkgdesc="A cross-platform open-source make system (vim mode)"
  depends=("cmake=$pkgver" 'vim')
  cd "${srcdir}/dest"
  vimpath="${pkgdir}/usr/share/vim/vimfiles"
  install -d "${vimpath}"/indent
  install -d "${vimpath}"/syntax

  cp -r "${srcdir}/dest/usr/share/${_realname}-$pkgver/editors/vim/indent/cmake.vim" "${vimpath}/indent/"
  cp -r "${srcdir}/dest/usr/share/${_realname}-$pkgver/editors/vim/syntax/cmake.vim" "${vimpath}/syntax/"
}

package_cmake-emacs() {
  pkgdesc="A cross-platform open-source make system (Emacs mode)"
  depends=("cmake=$pkgver" 'emacs')
  cd "${srcdir}/dest"

  install -d  ${pkgdir}/usr/share/${_realname}-$pkgver/editors/emacs/
  install -d "${pkgdir}/usr"/share/emacs/site-lisp/
  cp "usr/share/${_realname}-$pkgver/editors/emacs/cmake-mode.el" "${pkgdir}/usr"/share/emacs/site-lisp/

  /usr/bin/emacs -batch -f batch-byte-compile \
    "${pkgdir}/usr"/share/emacs/site-lisp/cmake-mode.el
}

